/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.world;

import cz.muni.fi.diplomka.servicelayer.dto.Tile;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.WorldManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.WorldManagerImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;

/**
 *
 * @author Jakub Kutil
 */
class TileProvider implements IDataProvider<Tile> {

    private WorldManager worldManager = new WorldManagerImpl();

    public TileProvider(Long id) {
    }

    @Override
    public Iterator<? extends Tile> iterator(int first, int count) {
        List<Tile> world = worldManager.getWorld();
        List<Tile> selectedworld = new ArrayList<>();
        Collections.sort(world, new Comparator<Tile>() {
            @Override
            public int compare(Tile tile1, Tile tile2) {

                if (tile1.getCoordinates().getX_coordinate() < tile2.getCoordinates().getX_coordinate()) {
                    return -1;
                } else {
                    if (tile1.getCoordinates().getY_coordinate() < tile2.getCoordinates().getY_coordinate()) {
                        return - 1;
                    }
                    if (tile1.getCoordinates().getY_coordinate() > tile2.getCoordinates().getY_coordinate()) {
                        return 1;
                    }
                }
                return 0;
            }
        });

        for (int i = first; i < count; i++) {
            selectedworld.add(world.get(i));
        }
        return selectedworld.iterator();}

    @Override
    public int size() {
        return worldManager.getWorld().size();
    }

    @Override
    public IModel<Tile> model(Tile object) {
        return new DetachableTileModel(object);
    }

    @Override
    public void detach() {
    }
}
